package com.itheima.stock.security.service;

import com.google.common.base.Strings;
import com.itheima.stock.mapper.SysPermissionMapper;
import com.itheima.stock.mapper.SysRoleMapper;
import com.itheima.stock.mapper.SysUserMapper;
import com.itheima.stock.pojo.SysPermission;
import com.itheima.stock.pojo.SysRole;
import com.itheima.stock.pojo.SysUser;
import com.itheima.stock.service.PermissionService;
import com.itheima.stock.vo.PermissionRespNodeVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.AuthorityUtils;
import org.springframework.security.core.userdetails.UserDetails;
import org.springframework.security.core.userdetails.UserDetailsService;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.stereotype.Component;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@Component
public class LoginUserDetailService implements UserDetailsService {
    @Autowired
    private SysUserMapper sysUserMapper;

    @Autowired
    private SysPermissionMapper sysPermissionMapper;

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Autowired
    private PermissionService permissionService;
    @Override
    public UserDetails loadUserByUsername(String userName) throws UsernameNotFoundException {
        //根据用户名查询用户信息
        SysUser user = sysUserMapper.findByUserName(userName);
        if(user==null){
            throw new UsernameNotFoundException("用户名不存在！！！");
        }
        //获取权限集合
        List<SysPermission> permissions = sysPermissionMapper.getPermissionByUserId(user.getId());
        //获取授权
        List<String> perms = permissions.stream().filter(permission -> !Strings.isNullOrEmpty(permission.getPerms())).map(permission -> permission.getPerms()).collect(Collectors.toList());
        //获取角色集合 基于角色鉴权注解需要将角色前追加ROLE_
        List<SysRole> roles= sysRoleMapper.getRoleByUserId(user.getId());
        List<String> roleNames = roles.stream().filter(role -> Strings.isNullOrEmpty(role.getName())).map(role -> "ROLE_" + role.getName()).collect(Collectors.toList());

        //将权限和角色都封装到auths中
        List<String> auths= new ArrayList<>();
        auths.addAll(perms);
        auths.addAll(roleNames);
        //转化为数组
        String[] permArr=auths.toArray(new String[auths.size()]);
        //转化为权限对象集合
        List<GrantedAuthority> authorityList = AuthorityUtils.createAuthorityList(permArr);
        user.setAuthorities(authorityList);

        //权限树结构，给前端响应--》根据用户拥有的权限获取对应侧边栏菜单数
        List<PermissionRespNodeVo> treeNodeVo = permissionService.getTree(permissions, "0", true);
        user.setMenus(treeNodeVo);

        //按钮权限集合，给前端响应
        List<String> authBtnPerms=null;
        if (!CollectionUtils.isEmpty(permissions)) {
            authBtnPerms = permissions.stream().filter(per -> !Strings.isNullOrEmpty(per.getCode()) && per.getType()==3)
                    .map(per -> per.getCode()).collect(Collectors.toList());
        }
        user.setPermissions(authBtnPerms);
        return user;
    }
}
